package com.cms.ctrl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.cms.domain.Data;
import com.cms.domain.DataRow;
import com.cms.domain.Item;
import com.cms.service.DataRowService;
import com.cms.service.DataService;
import com.cms.service.ItemService;
import com.core.page.Pagination;
import com.wxcms.domain.AccountFans;

/**
 * 
 */

@Controller
@RequestMapping("/data")
public class DataCtrl{

	@Autowired
	private ItemService itemService;
	
	@Autowired
	private DataService dataService;

	@Autowired
	private DataRowService dataRowService;
	
	@RequestMapping(value = "/dataManager")
	public  ModelAndView dataManager(String id_sbj,Pagination<AccountFans> pagination){
		ModelAndView modelAndView = new ModelAndView("subject/data/dataManager");
		
		modelAndView.addObject("id_sbj", id_sbj);
		
		//查询列头
		List<Item> itemList = itemService.getList(Long.valueOf(id_sbj));
		modelAndView.addObject("itemList", itemList);
		
		List pageList = dataRowService.getValuesList(Long.valueOf(id_sbj), pagination.getPageNum(), pagination.getPageSize());
		if(pageList!=null && pageList.size()>0){
			pagination.setItems(pageList);
			pagination.setTotalItemsCount(dataRowService.getRowCount(Long.valueOf(id_sbj)));
		}
		
		modelAndView.addObject("pagination", pagination);
		
		return modelAndView;
	}
	
	
//	@RequestMapping(value = "/dataManager")
//	public  ModelAndView dataManager(String id_sbj){
//		ModelAndView modelAndView = new ModelAndView("subject/data/dataManager");
//		
//		modelAndView.addObject("id_sbj", id_sbj);
//		
//		//查询列头
//		List<Item> itemList = itemService.getList(Long.valueOf(id_sbj));
//		modelAndView.addObject("itemList", itemList);
//		
//		HashMap<Long , Integer> item_column_index = new HashMap();
//		for(int i = 0 ;i<itemList.size(); i++){
//			Item item = itemList.get(i);
//			item_column_index.put(item.getId(), new Integer(i));
//		}
//		
//		List<DataRow> dataRowList = new ArrayList();
//		
//		//查询数据
//		List<Data> datas = dataService.getList(Long.valueOf(id_sbj));
//		HashMap<Integer , Integer> num_row_hm = new HashMap();
//		
//		for(Data data:datas){
//			int data_num = data.getRowNum().intValue();
//			if(item_column_index.get(data.getIdItem())!=null){
//				int index_column = item_column_index.get(data.getIdItem());
//				DataRow dataRow = new DataRow();
//				if(num_row_hm.get(data_num)!=null){
//					dataRow = dataRowList.get(num_row_hm.get(data_num));
//				}else{
//					dataRow.setDataList(new ArrayList());
//					dataRowList.add(dataRow);
//					num_row_hm.put(data_num, dataRowList.size()-1);
//				}
//				
//				for(int i=dataRow.getDataList().size();i<index_column;i++){
//					Item item = itemList.get(i);
//					Data data_new = new Data();
//					data_new.setIdSbj(item.getIdSbj());
//					data_new.setIdItem(item.getId());
//					data_new.setRowNum(data.getRowNum());
//					dataRow.getDataList().add(data_new);
//				}
//				dataRow.getDataList().add(data);
//			}
//			
//		}
//		
//		modelAndView.addObject("itemList", itemList);
//		modelAndView.addObject("dataRowList", dataRowList);
//		return modelAndView;
//	}

	@RequestMapping(value = "/toMerge")
	public  ModelAndView toMerge(String id_sbj,String rowNum){
		ModelAndView modelAndView = new ModelAndView("subject/data/dataMerge");
		modelAndView.addObject("id_sbj", id_sbj);
		modelAndView.addObject("rowNum", rowNum);
		
		//查询列头
		List<Item> itemList = itemService.getList(Long.valueOf(id_sbj));
		modelAndView.addObject("itemList", itemList);
		
		DataRow dataRow = new DataRow();
		dataRow.setDataList(new ArrayList());
		
		if(rowNum==null){
			for(int index =0 ; index<itemList.size(); index++){
				Item item = itemList.get(index);
				Data data_new = new Data();
				data_new.setIdSbj(item.getIdSbj());
				data_new.setIdItem(item.getId());
				dataRow.getDataList().add(data_new);
			}
		}else{
			//查询数据
			List<Data> datas = dataService.getRowList(Long.valueOf(id_sbj), Long.valueOf(rowNum));
			for(int index =0 ; index<itemList.size(); index++){
				Item item = itemList.get(index);
				boolean hasData = false;
				for(Data data:datas){
					if(item.getId().equals(data.getIdItem())){
						dataRow.getDataList().add(data);
						hasData = true;
					}
				}
				if(!hasData){
					Data data_new = new Data();
					data_new.setIdSbj(item.getIdSbj());
					data_new.setIdItem(item.getId());
					data_new.setRowNum(Long.valueOf(rowNum));
					dataRow.getDataList().add(data_new);
				}
			}
		}

		modelAndView.addObject("dataRow", dataRow);
		return modelAndView;
	}

	@RequestMapping(value = "/doMerge")
	public  ModelAndView doMerge(String id_sbj,String rowNum,DataRow dataList){
		
		if(rowNum==null || rowNum.equals("")){
			
			Long maxRowNum = dataService.getRowNumMax(Long.valueOf(id_sbj));
			
			Long rowNum_new = new Long(0);
			if(maxRowNum!=null){
				rowNum_new = maxRowNum+1;
			}
			
			for(Data data:dataList.getDataList()){
				data.setRowNum(rowNum_new);
				dataService.add(data);
			}
			rowNum = rowNum_new.toString();
		}else{
			for(Data data:dataList.getDataList()){
				if(data.getId()==null||data.getId().equals("")){
					dataService.add(data);
				}else{
					dataService.update(data);
				}
			}
		}
		
		return this.toMerge(id_sbj, rowNum);
		
	}
	
	@RequestMapping(value = "/delete")
	public  ModelAndView delete(String id_sbj,String rowNum,Pagination<AccountFans> pagination){
		
		List<Data> datas = dataService.getRowList(Long.valueOf(id_sbj), Long.valueOf(rowNum));
		for(Data data:datas){
			dataService.delete(data);
		}
		
		return this.dataManager(id_sbj,pagination);
	}
	
	
}

